'''
Created on 18/11/2011

@author: Byron
'''
from tkinter import *
from Constants import *
from Mensajero import Mensajero

class Chat:
    def __init__(self, ventana, pPlayer):
        self._Player = pPlayer
        #Crear canvas
        chatCanvas  = Canvas(ventana, width=280, height=440)
        #widget
        self._CanvasHistory  = Canvas(chatCanvas, width=280, height=380)
        self._CanvasHistory.place(x=5, y=20)
        self._TextChat  = Text(self._CanvasHistory, height=23, width=30)
        scroll = Scrollbar(self._CanvasHistory, command=self._TextChat.yview)
        self._TextChat.configure(yscrollcommand=scroll.set)
        self._TextChat.config(state=DISABLED)
        self._TextChat.pack(side=LEFT)
        scroll.pack(side=RIGHT, fill=Y)
        self._Entrada = Entry(chatCanvas, width=35)
        self._Entrada.place(x=5,y=400)
        self._BotonEnviar = Button(chatCanvas, text=BOTON_ENVIAR, command=self.enviar, bg=COLOR_BLACK, fg=COLOR_WHITE)
        self._BotonEnviar.place(x=225,y=395)
        #Ubicar canvas
        chatCanvas.place(x=650, y=0)
        
    def setMensajero(self,pMensajero):
        self._Mensajero = pMensajero
    
    def enviar(self):
        if(self._Entrada.get() != ""):
            self._Mensajero.send(CHAT+DIVISOR+self._Entrada.get())
            self._TextChat.config(state=NORMAL)
            self._TextChat.insert(END, self._Player+": "+self._Entrada.get()+"\n") 
            self._TextChat.config(state=DISABLED) 
            self._Entrada.delete(0, END)

    def imprimir(self,pMensaje):
        self._TextChat.config(state=NORMAL)
        self._TextChat.insert(END, "Strange"+": "+pMensaje+"\n")
        self._TextChat.config(state=DISABLED)
